home *** CD-ROM | disk | FTP | other *** search
- #include <RJS/String.h>
- #include "Regex.h"
-
- #include "GNUregex.h"
- // Regex stuff
-
- RJS_Regex::RJS_Regex()
- {
- compile_error="RE: not intialized";
- buf=0;
- reg=0;
- changed=0;
- }
-
- RJS_Regex::RJS_Regex(const RJS_Regex &re) : RJS_String(re.cptr())
- {
- initialize(re.cptr(), re.length(), (int) re.buf->fastmap,re.buf->allocated,
- re.buf->translate);
- }
-
- RJS_Regex::RJS_Regex(const RJS_String& s, int fast , int bufsize,
- const char* transtable ) : RJS_String(s)
- {
- initialize(s.cptr(), s.length(), fast, bufsize, transtable);
- }
-
- RJS_Regex::RJS_Regex(const char *s, int fast, int bufsize,
- const char* transtable) : RJS_String(s)
- {
- initialize(s, -1, fast, bufsize, transtable);
- }
-
- RJS_Regex::~RJS_Regex()
- {
- if (buf) {
- delete(buf->buffer);
- delete(buf->fastmap);
- }
- delete(buf);
- delete(reg);
- }
-
- void RJS_Regex::initialize(const char* t, int tlen, int fast, int bufsize,
- const char* transtable)
- {
- if (tlen < 0) tlen = RJS_String::length(t);
- buf = new re_pattern_buffer;
- reg = new re_registers;
- if (fast)
- buf->fastmap = new char[256];
- else
- buf->fastmap = 0;
- buf->translate = (char*)transtable;
- if (tlen > bufsize)
- bufsize = tlen;
- buf->allocated = bufsize;
- buf->buffer = new char [buf->allocated];
- compile_error=re_compile_pattern((char*)t, tlen, buf);
- if (!compile_error) if (fast) re_compile_fastmap(buf);
- changed=0;
- }
-
- int RJS_Regex::match_info(int& start, int& length, int nth) const
- {
- if ((unsigned)(nth) >= RE_NREGS)
- return 0;
- else
- {
- start = reg->start[nth];
- length = reg->end[nth] - start;
- return start >= 0 && length >= 0;
- }
- }
-
- int RJS_Regex::search(const char* s, int& matchlen, int len,int startpos) const
- {
- int matchpos, pos, range;
- if (changed) compile();
- if (compile_error) { matchlen=0; return -1; }
- if (len<0) len=RJS_String::length(s);
- if (startpos >= 0)
- {
- pos = startpos;
- range = len - startpos;
- }
- else
- {
- pos = len + startpos;
- range = -pos;
- }
- matchpos = re_search_2(buf, 0, 0, (char*)s, len, pos, range, reg, len);
- if (matchpos >= 0)
- matchlen = reg->end[0] - reg->start[0];
- else
- matchlen = 0;
- return matchpos;
- }
-
- int RJS_Regex::search(const RJS_String & s, int& matchlen) const
- {
- int matchpos, pos, range;
- int len=s.length();
- if (changed) compile();
- if (compile_error) { matchlen=0; return -1; }
- pos = 0;
- range = len;
- matchpos = re_search_2(buf, 0, 0, (char *) s.cptr(), len, pos, range, reg, len);
- if (matchpos >= 0)
- matchlen = reg->end[0] - reg->start[0];
- else
- matchlen = 0;
- return matchpos;
- }
-
- int RJS_Regex::match(const char *s, int len, int p) const
- {
- if (changed) compile();
- if (compile_error) { return 0; }
- if (len<0) len=RJS_String::length(s);
- if (p < 0)
- {
- p += len;
- if (p >= len)
- return 0;
- return re_match_2(buf, 0, 0, (unsigned char*)s, p, 0, reg, p);
- }
- else if (p >= len)
- return 0;
- else
- return re_match_2(buf, 0, 0, (unsigned char*)s, len, p, reg, len);
- }
-
- int RJS_Regex::match(const RJS_String &s) const
- {
- int len=s.length();
- if (changed) compile();
- if (compile_error) { return 0; }
- if (len==0) return 0;
- return re_match_2(buf, 0, 0, (unsigned char*)s.cptr(), len, 0, reg, len);
- }
-
- int RJS_Regex::compile()
- {
- int fast = (int)buf->fastmap;
- int alloc = buf->allocated;
- char *trans = buf->translate;
-
- if (buf) {
- delete(buf->buffer);
- delete(buf->fastmap);
- }
- delete(buf);
- delete(reg);
-
- initialize(cptr(),length(), fast, alloc,trans);
-
- return ok();
- }
-
- void RJS_Regex::operator=(const RJS_Regex &re)
- {
- if (buf) {
- delete(buf->buffer);
- delete(buf->fastmap);
- }
- delete(buf);
- delete(reg);
-
- (RJS_String &) *this = (RJS_String &) (re);
-
- initialize(re.cptr(), re.length(), (int) re.buf->fastmap,re.buf->allocated,
- re.buf->translate);
- }
-
- void RJS_Regex::operator=(const char *s)
- {
- if (buf) {
- delete(buf->buffer);
- delete(buf->fastmap);
- }
- delete(buf);
- delete(reg);
-
- (RJS_String &) *this = s;
- initialize(s, -1, 0,40,0); // SHOULD FIX!!! fast, bufsize, transtable);
- }
-
- void RJS_Regex::operator=(const RJS_String &s)
- {
- if (buf) {
- delete(buf->buffer);
- delete(buf->fastmap);
- }
- delete(buf);
- delete(reg);
-
- (RJS_String &) *this = s;
- initialize(s.cptr(), s.length(), 0,40,0); // fast, bufsize, transtable);
- }
-
- int RJS_Regex::ok() const
- {
- if (changed) compile();
- int v = buf != 0; // have a regex buf
- v &= buf->buffer != 0; // with a pat
- return v && (compile_error==0);
- }
-
- const RJS_Regex RXwhite("[ \n\t\r\v\f]+", 1);
- const RJS_Regex RXoptwhite("[ \n\t\r\v\f]*", 1);
- const RJS_Regex RXnonwhite("[^ \n\t\r\v\f]+", 1);
- const RJS_Regex RXint("-?[0-9]+", 1);
- const RJS_Regex RXdouble("-?\\(\\([0-9]+\\.[0-9]*\\)\\|\\([0-9]+\\)\\|\\(\\.[0-9]+\\)\\)\\([eE][---+]?[0-9]+\\)?", 1, 200);
- const RJS_Regex RXalpha("[A-Za-z]+", 1);
- const RJS_Regex RXlower("[a-z]+", 1);
- const RJS_Regex RXupper("[A-Z]+", 1);
- const RJS_Regex RXalphanum("[0-9A-Za-z]+", 1);
- const RJS_Regex RXid("[A-Za-z_][A-Za-z0-9_]*", 1);
- const RJS_Regex RXstr("\"[^\"]*\"",1);
- // RXstrq is a string with \" in it. returns string with \" included
- const RJS_Regex RXstrq("\"\\([^\"\\]\\|\\\\\"\\|\\\\\\)*\"",1);
-
-